package org.joy.hpc.analyzer.examples.sogou;

import java.io.IOException;
import java.io.StringReader;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.cyberneko.html.parsers.DOMParser;
import org.joy.analyzer.html.HTMLDocument;
import org.joy.analyzer.html.ParseException;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * 搜狗HTML语料文档类，比HTML文档多了一个ID属性
 * 
 * @author andy
 * 
 */
public class SogouDocument extends HTMLDocument {

	private String docID;

	public static SogouDocument createSogouDocument(SogouDocumentInput doc)
			throws ParseException {
		DOMParser parser = new DOMParser();
		try {
			parser.parse(new InputSource(new StringReader(doc.getHTMLContent().trim())));
		} catch (SAXException ex) {
			// parsing fail
			Logger.getLogger(HTMLDocument.class.getName()).log(Level.SEVERE,
					null, ex);
			throw new ParseException(ex.getMessage());
		} catch (IOException ex) {
			// never reach here
			Logger.getLogger(HTMLDocument.class.getName()).log(Level.SEVERE,
					null, ex);
		} catch (Exception e) {
			System.out.println("解析失败！" + e.getMessage() + doc.getDocumentId());
		}
		return new SogouDocument(doc.getURL(), doc.getHTMLContent(), parser
				.getDocument(), doc.getDocumentId());
	}

	protected SogouDocument(String url, String str, Document doc, String docID)
			throws ParseException {
		super(url, str, doc);
		// set the document ID for our sogou Document
		this.docID = docID;
	}

	public String getDocumentID() {
		return docID;
	}
}
